package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebScoreRecordDao;
import ysq.xyg.web.model.po.WebScoreRecord;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebScoreRecordVo;
import ysq.xyg.web.service.WebScoreRecordService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-积分记录service实现
 *
 * @author shengQiang yu
 * @date 2019-7-16 9:16:11
 */
@Service("webScoreRecordService")
public class WebScoreRecordServiceImpl extends BaseServiceImpl<WebScoreRecord> implements WebScoreRecordService {

    @Resource
    private WebScoreRecordDao webScoreRecordDao;

    private WebScoreRecord webScoreRecord;

    @Autowired
    public void setBaseDao(WebScoreRecordDao webScoreRecordDao) {
        this.baseDao = webScoreRecordDao;
    }

    /**
     * 关联查询
     *
     * @param webScoreRecordVo 对象
     * @author shengQiang yu
     * @date 2019-04-02 12:42
     */
    public List<WebScoreRecord> listLeft(WebScoreRecordVo webScoreRecordVo){
        return webScoreRecordDao.listLeft(webScoreRecordVo);
    }
    /**
     * 分页查询
     *
     * @param webScoreRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 9:16:11
     */
    public ReturnEntity page(WebScoreRecordVo webScoreRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebScoreRecord> list = listLeft(webScoreRecordVo);
        PageInfo<WebScoreRecord> page=new PageInfo<WebScoreRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webScoreRecordVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 9:16:11
     */
    public ReturnEntity addOrUpdate(WebScoreRecordVo webScoreRecordVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webScoreRecordVo.getId())){
            webScoreRecord=new WebScoreRecord(currentUser);
            isUpate=false;
        }else{
            webScoreRecord=selectByPrimaryKey(webScoreRecordVo.getId());
            if(ObjectUtil.isNull(webScoreRecord)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webScoreRecord.setUpdateTime(new Date());
            webScoreRecord.setUpdateUser(currentUser.getId());
        }
        webScoreRecord.setMemberId(webScoreRecordVo.getMemberId());
        webScoreRecord.setScore(webScoreRecordVo.getScore());
        webScoreRecord.setRecordType(webScoreRecordVo.getRecordType());
        webScoreRecord.setDescripts(webScoreRecordVo.getDescripts());
        webScoreRecord.setExtraId(webScoreRecordVo.getExtraId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webScoreRecord);
        }else{
            num=insert(webScoreRecord);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 9:16:12
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebScoreRecord webScoreRecord=selectByPrimaryKey(id);
        if(webScoreRecord!=null){
            webScoreRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webScoreRecord.setUpdateTime(new Date());
            webScoreRecord.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webScoreRecord);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webScoreRecordVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-16 9:16:12
     */
    public List<WebScoreRecord> acceptList(WebScoreRecordVo webScoreRecordVo){
        Example example = new Example(WebScoreRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webScoreRecordVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webScoreRecordVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webScoreRecordDao.selectByExample(example);
    }
}
